package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: SetmealServiceImpl
 * Package: com.sky.service.impl
 * Description:
 *
 * @Autehor 屈子岩
 * @Create 2024/9/10 15:44
 * @Version 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<Setmeal> setmealPage = setmealMapper.pageQuery(setmealPageQueryDTO);
        List<SetmealVO> collect = setmealPage.getResult().stream().map(item -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(item, setmealVO);
            setmealVO.setCategoryName(categoryMapper.selectById(item.getCategoryId()));
            return setmealVO;
        }).collect(Collectors.toList());
        PageResult pageResult = new PageResult(setmealPage.getTotal(), collect);
        return pageResult;
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(StatusConstant.ENABLE);
        setmealMapper.insert(setmeal);

        Long id = setmeal.getId();

        setmealDTO.getSetmealDishes().forEach(item -> item.setSetmealId(id));
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishMapper.insertBatch(setmealDishes);
        //可以优化为以上方式
//        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
//        for (SetmealDish setmealDish : setmealDishes){
//            setmealDish.setSetmealId(id);
//            setmealDishDao.insert(setmealDish);
//        }

    }


    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        setmealVO = setmealMapper.getById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐状态
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();

        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        for (SetmealDish setmealDish : setmealDishes ){
            DishVO dish = dishMapper.getById(setmealDish.getDishId());
            Integer dishStatus = dish.getStatus();
            if(dishStatus == StatusConstant.DISABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }

        setmealMapper.update(setmeal);
    }


    /**
     * 修改套餐信息
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        setmealDTO.getSetmealDishes().forEach(item->item.setSetmealId(setmealDTO.getId()));
        setmealDishMapper.insertBatch(setmealDTO.getSetmealDishes());

    }

    /**
     * 批量删除套餐信息
     * @param ids
     * @return
     */
    @Override
    public void deleteById(List<Long> ids) {

        for (Long id : ids){
            SetmealVO setmealVO = setmealMapper.getById(id);
            Integer status = setmealVO.getStatus();
            //判断套餐是否在售
            if (status == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            setmealMapper.deleteById(id);
            setmealDishMapper.deleteBySetmealId(id);
        }


    }

    /**
     * 用户根据套餐id查询套餐信息
     * @param setmealDTO
     * @return
     */
    @Override
    public List<Setmeal> list(SetmealDTO setmealDTO) {
        List<Setmeal> setmealList = setmealMapper.getBycategoryId(setmealDTO.getCategoryId());
        return setmealList;

    }

    /**
     *
     * 用户根据套餐id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getByIdWithDish(Long id) {
        List<DishItemVO> dishItemVOS = new ArrayList<>();
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        setmealDishes.stream().forEach(item->{
            DishVO dish = dishMapper.getById(item.getDishId());
            DishItemVO dishItemVO = DishItemVO.builder()
                    .copies(item.getCopies())
                    .name(item.getName())
                    .image(dish.getImage())
                    .description(dish.getDescription())
                    .build();
            dishItemVOS.add(dishItemVO);
        });
        return dishItemVOS;
    }
}
